/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-6-4上午09:28:07
 */
package hit.edance.campusmobile.chat.database;

import java.util.ArrayList;
import java.util.List;

import hit.edance.campusmobile.chat.model.ChatItem;
import hit.edance.campusmobile.chat.model.ChatMessage;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**   
 * 聊天记录DB操作
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.database
 * @Class DBChatHistoryHelper
 * @Date 2012-6-4 上午09:28:07
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class DBChatHistoryHelper {
	private static final String DEBUG_TAG = "DbChatHistoryHelper"; 
	
	private static final String TABLE_NAME = "ChatHistory";  //表名
	
	private final Context mCtx;
	
	private DBUtil db;
	
	private SQLiteDatabase mDb;	//声明数据库对象
	
	/**
	 * 构造方法中初始化Context对象
	 * @param ctx
	 */
	public DBChatHistoryHelper(Context ctx) {
		this.mCtx = ctx;
		db = new DBUtil(mCtx);
	}

	/**
	 * 向数据库中插入ChatItem
	 * 
	 * @param account
	 * @return 成功或失败
	 */
	public boolean insertChatHistoryItem(ChatItem chatItem) {
		final String sql = "insert into " + TABLE_NAME
				+ " (myid,content,time,itsid,tag) values('"
				+ chatItem.getMyid() + "','" + chatItem.getContent() + "','"
				+ chatItem.getTime() + "','" + chatItem.getItsid() + "','"
				+ chatItem.getTag() + "');";
		try {
			mDb = db.open().getWritableDB();
			mDb.execSQL(sql);
			Log.i(DEBUG_TAG + ":" + TABLE_NAME, sql);
			return true;
		} catch (SQLException e) {
			return false;
		} finally {
			db.close();
		}

	}
	/**
	 * 清除聊天历史
	 * @param myid
	 * @param itsid
	 * @return
	 */
	public boolean clearHistory(String myid,String itsid){
		final String sql = "DELETE FROM "+TABLE_NAME+" WHERE myid='"+myid+"' AND itsid='"+itsid+"';";
		try{
			mDb = db.open().getWritableDB();
			mDb.execSQL(sql);
			Log.i(DEBUG_TAG + ":" + TABLE_NAME, sql);
			return true;
		}catch(SQLException e){
			return false;
		}finally{
			db.close();
		}
		
	}
	/**
	 * @return 返回所有的ChatItem的Cursor
	 */
	public List<ChatMessage> getAllChatItems(String myid,String itsid) {
		Cursor cursor = null;
		try {
			mDb = db.open().getReadableDB();
			cursor = mDb.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE myid='"+myid+"' AND itsid='"+itsid+"';",null);
			
			List<ChatMessage> list = new ArrayList<ChatMessage>();
			
			int content = cursor.getColumnIndex("content");
			int tag = cursor.getColumnIndex("tag");
			for(cursor.moveToFirst(); ! cursor.isAfterLast(); cursor.moveToNext()){
				list.add(new ChatMessage(Integer.parseInt(cursor.getString(tag))
						, cursor.getString(content)));
			}
			return list;

		} finally {
			if(cursor != null)
				cursor.close();
			db.close();
		}
	}
}
